﻿//poi
var poiItems;

function PoiService(layerInfo) {
    var PoiServiceLayer = new Object;
    PoiServiceLayer.id = layerInfo.hasOwnProperty("GUID") ? layerInfo.GUID : "";
    PoiServiceLayer.position = layerInfo.hasOwnProperty("POSITION") ? layerInfo.POSITION : "";
    PoiServiceLayer.url = layerInfo.hasOwnProperty("URL") ? layerInfo.URL.replace("http://:", "http://") : "";
    PoiServiceLayer.name = layerInfo.hasOwnProperty("NAME") ? layerInfo.NAME : "";
    PoiServiceLayer.servertype = layerInfo.hasOwnProperty("SERVERTYPE") ? layerInfo.SERVERTYPE : "";
    PoiServiceLayer.CreateLayer = function() {
        var url = this.url.replace("http://http://", "http://");
        var labelField = "Name";
        var labelFont = "13px";
        var labelColor = "#ffffff";
        var labelBackground = "#0000ff";
        var labelHeight = -50;
        Cesium.GeoJsonDataSource.load(url, {
            // clampToGround: true
        }).then(function(dataSource) {
            var entities = dataSource.entities.values;
            for (var i = 0; i < entities.length; i++) {
                var entity = entities[i];
                var position = entity.position._value;
                var cartographic = Cesium.Cartographic.fromCartesian(position);
                var longitude = Cesium.Math.toDegrees(cartographic.longitude);
                var latitude = Cesium.Math.toDegrees(cartographic.latitude);
                // var height = viewer.scene.sampleHeight(new Cesium.Cartographic(position.x, position.y));
                // var position = entity.position.getValue(Cesium.JulianDate.now());
                //entity.position = viewer.scene.clampToHeight(position);
                //entity.position = Cesium.Cartesian3.fromDegreesArrayHeights([longitude, latitude, Cesium.Cartographic.fromCartesian(viewer.scene.clampToHeight(position)).height + 2])[0];
                entity.billboard = undefined;
                entity.point = {
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                    pixelSize: 10,
                    color: Cesium.Color.RED.withAlpha(0)
                }
                entity.label = {
                    text: entity._properties.getValue("")[labelField],
                    font: labelFont,
                    fillColor: !labelColor ? Cesium.Color.WHITE : Cesium.Color.fromCssColorString(labelColor),
                    style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                    showBackground: !labelBackground ? false : true,
                    backgroundColor: !labelBackground ? Cesium.Color.GREEN : Cesium.Color.fromCssColorString(labelBackground),
                    backgroundPadding: new Cesium.Cartesian2(3, 3),
                    pixelOffset: new Cesium.Cartesian2(0, labelHeight),
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
                };
            }
            thisLayer = dataSource;
            viewer.dataSources.add(thisLayer);
            thisLayer.NAME = poiItems.name;
            cesium.layer3DList[poiItems.id] = thisLayer;
        }.bind(this));
        return {};
    }
    poiItems = PoiServiceLayer;
    return PoiServiceLayer;
}